package com.capgemini.scaffold.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.capgemini.scaffold.common.BusinessException;
import com.capgemini.scaffold.common.ExceptionEnum;
import com.capgemini.scaffold.mapper.ProjectInfoMapper;
import com.capgemini.scaffold.mapper.ProjectMemberMapper;
import com.capgemini.scaffold.mapper.UserMapper;
import com.capgemini.scaffold.model.ProjectInfo;
import com.capgemini.scaffold.model.ProjectMember;
import com.capgemini.scaffold.model.ProjectSchedule;
import com.capgemini.scaffold.model.User;
import com.capgemini.scaffold.model.request.ProjectQueryReq;
import com.capgemini.scaffold.model.request.ProjectReq;
import com.capgemini.scaffold.service.ProjectService;
import com.capgemini.scaffold.mapper.ProjectScheduleMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* @author 11470
* @description 针对表【project_schedule(项目进度表)】的数据库操作Service实现
* @createDate 2022-10-18 16:15:02
*/
@Service
public class ProjectServiceImpl extends ServiceImpl<ProjectInfoMapper,ProjectInfo>
    implements ProjectService {
    @Resource
    private UserMapper userMapper;
    @Resource
    private ProjectInfoMapper projectInfoMapper;
    @Resource
    private ProjectMemberMapper projectMemberMapper;
    @Resource
    private ProjectScheduleMapper projectScheduleMapper;

    /**
     * 查询项目信息列表
     *
     * @param projectQueryReq
     * @return
     */
    @Override
    public List<ProjectInfo> queryProject(ProjectQueryReq projectQueryReq) {

        QueryWrapper<ProjectInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("project_name",projectQueryReq.getProjectName())
                    .like("project_start_time",projectQueryReq.getProjectStartTime());
        int count = projectInfoMapper.selectCount(queryWrapper);
        if(count == 0){
            throw new BusinessException(ExceptionEnum.PARAMS_ERROR,"找不到该项目信息");
        }
        List<ProjectInfo> list = projectInfoMapper.selectList(queryWrapper);
        return list;
    }

    /**
     * 添加项目信息
     * @param
     * @return
     */
    public ProjectReq addProject(ProjectReq projectReq) {

        ProjectInfo projectInfo = projectReq.getProjectInfo();

        List<ProjectMember> projectMembers = projectReq.getProjectMembers();

        List<ProjectSchedule> projectSchedules = projectReq.getProjectSchedules();

        //获取系统当前时间，即为默认创建时间和更新时间
        Date date = new Date();
        projectInfo.setProjectUpdateTime(date);
        //查重
        QueryWrapper<ProjectInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("project_name", projectInfo.getProjectName());
        if(projectInfo.getProjectId()==0){
            QueryWrapper<ProjectInfo> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.orderByDesc("project_id").last("limit 1");
            projectInfo.setProjectId(projectInfoMapper.selectOne(queryWrapper1).getProjectId()+1);
        }
        //插入数据
        int saveResult = projectInfoMapper.insert(projectInfo);
        for (ProjectMember projectMember : projectMembers) {
            User user =userMapper.selectById(projectMember.getUserId());
            user.setStatusId("o");
            user.setProjectId(projectInfo.getProjectId());
            userMapper.updateById(user);
            projectMember.setProjectId(projectInfo.getProjectId());
            saveResult += projectMemberMapper.insert(projectMember);
        }
        for (ProjectSchedule projectSchedule : projectSchedules) {
            projectSchedule.setProjectId(projectInfo.getProjectId());
            saveResult += projectScheduleMapper.insert(projectSchedule);
        }
        if(saveResult == 0){
            throw new BusinessException(ExceptionEnum.PARAMS_ERROR,"保存失败");
        }
        return projectReq;
    }

    /**
     * 删除项目
     * @param projectId
     * @return
     */
    public boolean deleteProject(Integer projectId){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("project_id",projectId);
        List<User> users=userMapper.selectList(queryWrapper);
        for (User user: users) {
            user.setProjectId(null);
            user.setStatusId("b");
            userMapper.updateById(user);
        }
        Integer deResult1 = projectInfoMapper.deleteById(projectId);
        Integer deResult2 = projectScheduleMapper.deleteById(projectId);
        Integer deResult3 = projectMemberMapper.deleteById(projectId);
//        if(deResult1 != 1 || deResult2 != 1 || deResult3 != 1){
//            throw new BusinessException(ExceptionEnum.PARAMS_ERROR,"删除失败");
//        }
        throw new BusinessException(ExceptionEnum.SUCCESS);
    }

    /**
     * 获取ProjectInfo列表
     * @return
     */
    public List<ProjectInfo> getProjectInfo(){
       return projectInfoMapper.selectList(null);
    }

    /**
     * 获取ProjectMember列表
     * @return
     */
    public List<ProjectMember> getProjectMember(){
        return projectMemberMapper.selectList(null);
    }

    /**
     * 获取ProjectSchedule列表
     * @return
     */
    public List<ProjectSchedule> getProjectSchedule(){
        return projectScheduleMapper.selectList(null);
    }
}




